package com.tgd.RingtoneContactManager;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class GroupFragment extends ListFragment {

    private SimpleActivity _activity;
    private DAO _dao;
    private Group mSelectedGroup;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        _activity = (SimpleActivity) getActivity();

        _dao = DAO.getInstance(_activity);

        setHasOptionsMenu(true);

        GroupAdapter ea = new GroupAdapter(_activity, _dao.getGroups(Group.Filter.ALL,
                Group.Sort.NAME));
        setListAdapter(ea);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.group_actions, menu);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        mSelectedGroup = ((GroupAdapter.ViewHolder) v.getTag()).group;
        if (_actionMode == null) {
            _actionMode = _activity.startActionMode(mActionModeCallback);
        }
        _actionMode.setTitle(mSelectedGroup.getName());

        // TODO conditionally set context item visibility depending upon
        // applicability
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        GroupAdapter ga;

        switch (item.getItemId()) {
        case R.id.groupFilterAll:
        case R.id.groupFilterDefault:
        case R.id.groupFilterCustom:
        case R.id.groupFilterMixed:
            // TODO
            break;
        case R.id.groupSortContactCount:
        case R.id.groupSortAccountName:
        case R.id.groupSortGroupName:
            // TODO
            break;
        default:
            break;
        }

        getListView().invalidateViews();
        item.setChecked(true);
        return true;
    }

    private ActionMode _actionMode;
    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.group_context, menu);

            return true;
        }

        // Called each time the action mode is shown. Always called after
        // onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.groupPlay:
                // TODO
                break;
            case R.id.groupStop:
                _dao.stop();
                break;
            case R.id.groupSetRingtone:
                // TODO
            case R.id.groupSetAsDefault:
                // TODO
                break;
            default:
                return false;
            }
            return true;
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO the DAO should be able to tell me if a change was made, if
            // not then don't reload the adapter.
            // ContactAdapter ea = new ContactAdapter(_activity,
            // _dao.getContacts());
            // setListAdapter(ea);
            getListView().clearChoices();
            getListView().invalidateViews();
            _actionMode = null;
        }
    };
}
